package com.doubletick.proxy.liao;

import com.doubletick.common.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpStatus;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.StringJoiner;

@Slf4j
public class HttpTest {
    public static void main(String[] args) throws Exception {
        String result =null ;

        result = getResponseWithHttpProxy("995eaa21dae5c57f.fkz.as.ipidea.online", 2333,
                "ipidea_june-zone-custom-region-us-session-inqsb2yhe-sessTime-5", "ipidea_june",
                "https://api.ipify.org/?format=json");
//                "https://cip.cc");
        System.out.println(result);

        System.out.println("====================================================================");


        result= getResponseWithHttpProxy("bra.ppfly.xyz", 1081,
                "feng005_c1-res-US-session-74720844-sessTime-5", "feng005",
                "https://api.ipify.org/?format=json");
//                "https://cip.cc");
        System.out.println(result);


    }

    public static String getResponseWithHttpProxy(String proxyHost, int proxyPort, String proxyUser, String proxyPass, String requestUrl) throws Exception {
        // 创建CredentialsProvider并添加代理认证信息
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(
                new AuthScope(proxyHost, proxyPort),
                new UsernamePasswordCredentials(proxyUser, proxyPass));

        // 创建HttpClient并设置代理和CredentialsProvider
        CloseableHttpClient httpClient = HttpClients.custom()
                .setDefaultCredentialsProvider(credsProvider)
                .setProxy(new HttpHost(proxyHost, proxyPort))
                .build();

        try (CloseableHttpResponse response = httpClient.execute(new HttpGet(requestUrl))) {
            // 处理响应...
            response.getStatusLine().toString();
            log.info("{}", response.getStatusLine());
            int statusCode = response.getStatusLine().getStatusCode();
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                StringJoiner sj = new StringJoiner("\n");
                try (BufferedReader reader = new BufferedReader(
                        new InputStreamReader(entity.getContent(), StandardCharsets.UTF_8))) {
                    String line;
                    while ((line = reader.readLine()) != null) {
                        sj.add(line);
                    }
                }
                if (HttpStatus.SC_OK != statusCode) {
                    throw new ServiceException("失败：" + sj);
                }
                return sj.toString();
            }
            throw new ServiceException("失败：" + statusCode);
        } finally {
            // 关闭HttpClient
            httpClient.close();
        }
    }
}
